﻿using Newtonsoft.Json;
using T.ServiceComponent.Common.Service;

namespace T.FAS.Runtime.DataAccess.SQLDom
{
    /// <summary>
    /// 配置
    /// </summary>
    public sealed class ConfigProvider
    {
        public const string IsMasterNode = "FAS.IsMasterNode";
        private static ConfigProvider instance = new ConfigProvider();
        private static readonly object _lock = new object();
        private static SYSMaskGlobalSetting sysMaskGlobalSetting = null;

        /// <summary>
        /// 是否启动配置中心
        /// </summary>
        private static bool IsEnabled;

        private ConfigProvider()
        {
            IsEnabled = true;
        }

        public static ConfigProvider Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// 是否启用unicode编码
        /// </summary>       
        public bool TTPCoreDbEnableUnicode
        {
            get
            {
                try
                {
                    if (IsEnabled)
                    {
                        var val = ConfigHelper.GetBool("TTP.Core.DbEnableUnicode");
                        if (val.HasValue)
                            return val.Value;
                        else
                            return false;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// sys掩码全局开关-暂时不用，使用sdk
        /// </summary>       
        private SYSMaskGlobalSetting SYSMaskGlobalSetting
        {
            get
            {
                if (sysMaskGlobalSetting != null) return sysMaskGlobalSetting;
                try
                {
                    lock (_lock)
                    {
                        if (sysMaskGlobalSetting == null)
                        {
                            if (IsEnabled)
                            {
                                var val = ConfigHelper.GetString("BIZ.SYS.GlobalMask");
                                if (!string.IsNullOrEmpty(val))
                                {
                                    sysMaskGlobalSetting = JsonConvert.DeserializeObject<SYSMaskGlobalSetting>(val);
                                    return sysMaskGlobalSetting;
                                }
                            }
                        }
                        else
                            return sysMaskGlobalSetting;
                    }
                }
                catch
                { 
                }
                sysMaskGlobalSetting = new SYSMaskGlobalSetting { Switch = false };
                return sysMaskGlobalSetting;
            }
        }

        public bool EnableGlobalMask
        {
            get
            {
                return Maskservice.Current.EnableGlobalMask;
            }
        }
    }
}
